
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:whoisdinting/data.dart';
import 'package:whoisdinting/global.dart';

class GamePage extends StatefulWidget{
  @override
  _GamePageState createState() => _GamePageState();

}

class _GamePageState extends State<GamePage>{
  // int personCount=3;
  // WordPair wp;
  // String commonWord;
  // String dintWord;
  // int dintIndex;
  bool isOver;//是否结束
  String tips;//游戏结束提示语
  bool isView;//是否查看结果
  List<Person> persons;

  _GamePageState(){
    init();
  }
  void init(){
    isOver=false;
    isView=false;
  }
  @override
  Widget build(BuildContext context) {
    persons= ModalRoute.of(context).settings.arguments;
    //下划线widget预定义以供复用。
    Widget divider1=Divider(color: Colors.blue,);
    Widget divider2=Divider(color: Colors.green);
    var lv= ListView.separated(
      itemCount: Global.personCount,
      //列表项构造器
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          //上下各添加8像素补白
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: FlatButton(
              color: Colors.blue,
              disabledColor: Colors.grey,
              highlightColor: Colors.blue[700],
              colorBrightness: Brightness.dark,
              splashColor: Colors.grey,
              child: Text(getViewText(index)),
              onPressed: persons[index].isOut ? null : () async{
                if(isOver || isView){

                }else{
                  bool r=await showOutDialog(index);
                  if(r){
                    setPersonOut(index);
                    setState(() {
//                    persons[index].isOut=true;
                    });
                  }
                }
              },
            ),
        );
      },
      //分割器构造器
      separatorBuilder: (BuildContext context, int index) {
        return index%2==0?divider1:divider2;
      },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text("游戏页"),
      ),
      body: Center(
          child: Column(
            children: <Widget>[
              Text(isOver? tips: "",
                style: TextStyle(
                  fontSize: 18.0,
                ),
              ),
              Expanded(
                child: lv,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  FlatButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    child: Text("查看结果"),
                    shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                    onPressed: () async{
                      setState(() {
                        isView=true;
                      });
                    },
                  ),
                  SizedBox(width: 20,),
                  FlatButton(
                    color: Colors.blue,
                    highlightColor: Colors.blue[700],
                    colorBrightness: Brightness.dark,
                    splashColor: Colors.grey,
                    child: Text("重新开始"),
                    shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                    onPressed: () async{
                      if(isOver || isView){
                        Navigator.pushNamedAndRemoveUntil(context, "/", (route) => false);
                      }else{
                        bool restart=await showRestartDialog();
                        if(restart){
                          Navigator.pushNamedAndRemoveUntil(context, "/", (route) => false);
                        }
                      }
                    },
                  ),
                ],
              ),
            ],
        ),
      ),

    );
  }
  void setPersonOut(int index){
    persons[index].isOut=true;
    if(persons[index].isDint){
      isOver=true;
      tips="平民胜利";
    }else{
      int notOutNum=0;
      persons.forEach((element) {
        if( ! element.isOut){
          notOutNum++;
        }
      });
      if(notOutNum == 2){
        isOver=true;
        tips="卧底胜利";
      }
    }
  }

  Future<bool> showOutDialog(index) {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: Text((index+1).toString()+"玩家出局吗？"),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () => Navigator.of(context).pop(false), // 关闭对话框
            ),
            FlatButton(
              child: Text("确定"),
              onPressed: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }
  Future<bool> showRestartDialog() {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: Text("游戏未结束，确定重新开始吗？"),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () => Navigator.of(context).pop(false), // 关闭对话框
            ),
            FlatButton(
              child: Text("确定"),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }
  String getWord(int index){
    return persons[index].word;
  }
  String getViewText(int index){
    String vt="玩家 "+(index+1).toString();
    if(isOver || isView){
      vt=vt+" : "+getWord(index);
      if(persons[index].isDint){
        vt=vt+" (卧底)";
      }else{
        vt=vt+" (平民)";
      }
    }
    return vt;
  }
}

